package com.homwee.serialportinspection.util;

import android.app.Instrumentation;
import android.util.Log;

import com.mediatek.twoworlds.factory.MtkTvFApiInformation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Util {

    private static Class<?> mClassType = null;

    private static Method mGetMethod = null;

    private static Method mGetIntMethod = null;

    private static Method[] methods;

    public static int getSdkVersion() {
        return getInt("ro.build.version.sdk", -1);
    }


    public static void resetini() {
        MtkTvFApiInformation.getInstance().setIniParameter(
                Contants.PATH_FACTORYINI, Contants.FACTORY_M_MODE, "0");
        MtkTvFApiInformation.getInstance().setIniParameter(
                Contants.PATH_FACTORYINI, Contants.FACTORY_AGE_MODE, "0");
        MtkTvFApiInformation.getInstance().setIniParameter(
                Contants.PATH_FACTORYINI, Contants.KEY_UPGRADE_FORCE, "0");
    }

    public static boolean sendKeyEvent(final int keyCode) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Instrumentation instrumentation = new Instrumentation();
                instrumentation.sendKeyDownUpSync(keyCode);
            }
        }).start();
        return true;
    }

    public static String get(String key) {
        init();
        String value = null;
        try {
            value = (String) mGetMethod.invoke(mClassType, key);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        for (Method method : methods) {
            Log.i("*******Method********", method.toString());
        }
        return value;
    }

    public static int getInt(String key, int def) {
        init();
        int value = def;
        try {
            Integer v = (Integer) mGetIntMethod.invoke(mClassType, key, def);
            value = v.intValue();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return value;
    }

    private static void init() {
        try {
            if (mClassType == null) {
                mClassType = Class.forName("android.os.SystemProperties"); //通过Class.forname获取该Class对象;
                mGetMethod = mClassType.getDeclaredMethod("get", String.class);
                mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);
                methods = mClassType.getMethods();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
